<template>
    <div class="ele-body">
      <!-- 搜索表单 -->
      <saleproductSearch @search="reload"  />
      <!-- 数据表格 -->
      <el-card shadow="never">
        <ele-pro-table
          ref="table"  row-key="id" :columns="columns" :datasource="datasource"  :need-page="true"  show-summary :summary-method="getSummaries"  :cell-style="cellStyle"  @done="onDone"  :height="tableHeight" :full-height="tableFullHeight"
        >
          <!-- 表头工具栏 -->
          <template slot="toolbar">
            <el-button
              size="small"
              icon="el-icon-upload2"
              class="ele-btn-icon"
              @click="reportexcel"
            >
              导出
            </el-button>
          </template>
          <!-- 操作列 -->
          <template slot="action" slot-scope="{ row }"  >
            <el-link type="info"  :underline="false"  icon="el-icon-menu" @click="openEdit(row)" >明细</el-link>
          </template>
        </ele-pro-table>
      </el-card>
      <!-- 编辑弹窗 -->
      <saleproductDetail :data="current" :visible.sync="showEdit" @done="reload" />
      <!-- 导出数据 -->
      <ExportDialog ref="ExportDialog"></ExportDialog>
    </div>
  </template>
  
  <script>
    
    import { removePageTab } from '@/utils/page-tab-util';
    import ExportDialog from '@/components/Excel/ExportDialog'
    import saleproductSearch from './components/saleproduct-search.vue';
    import saleproductDetail from './components/saleproduct-detail.vue';
    import { QuerySaleproductdetailSummaryViewByCondition } from '@/api/statistics';


    export default {
      name: 'Saleproduct',
      components: {saleproductSearch,saleproductDetail,ExportDialog},
      data() {
        return {
          // 表格列配置
          columns: [
            {columnKey: 'index', type: 'index', width: 50, align: 'center', showOverflowTooltip: true, fixed: 'left', label:'序号',},
            {
              prop: 'outDate', label: '出库日期', width: 120, showOverflowTooltip: true, align: 'center',sortable: 'outDate',
              formatter: (row, column, cellValue) => {return this.$util.toDateString(cellValue,"YYYY-MM-DD");}
            },
            {prop: 'outOrderNo', label: '出库单号', showOverflowTooltip: true,align: 'center',},
            {prop: 'orderNo', label: '订单编号', showOverflowTooltip: true,align: 'center',},
            {prop: 'customerNo', label: '客户编码', width: 120,showOverflowTooltip: true,align: 'center',},
            {prop: 'customerName', label: '客户', showOverflowTooltip: true,align: 'center',},
            {prop: 'customerTypeDesc', label: '客户类别', width: 120,showOverflowTooltip: true,align: 'center',},
            {prop: 'productCode', label: '产品编号', showOverflowTooltip: true,align: 'center',},
            {prop: 'productName', label: '产品名称', showOverflowTooltip: true,align: 'center',},
            {prop: 'productTypeDesc', label: '产品类型', showOverflowTooltip: true,align: 'center',},
            {prop: 'spc', label: '规格', showOverflowTooltip: true,align: 'center' },
            {prop: 'unitDesc', label: '单位', showOverflowTooltip: true, align: 'center'},
            {prop: 'qty', label: '数量', showOverflowTooltip: true, align: 'center',},
            {prop: 'price', label: '单价', showOverflowTooltip: true,align: 'center',},
            {prop: 'amount', label: '金额', showOverflowTooltip: true,align: 'center',},
            {prop: 'warehouseDesc', label: '仓库', showOverflowTooltip: true,align: 'center', },
            // {columnKey: 'action', label: '操作', width: 80, align: 'center', resizable: false,slot: 'action', hideInSetting: true, showOverflowTooltip: true}
          ],
          //是否详情
          fixedHeight:true,
          // 当前编辑数据
          current: null,
          // 是否显示编辑弹窗
          showEdit: false,

          resvQtyTotal:0,
          saleQtyTotal:0,
          saleAmountTotal:0,

          // 导出列
          exportList: [
            { name: "出库日期", keys: "outDate", status: true },
            { name: "出库单号", keys: "outOrderNo", status: true },
            { name: "订单编号", keys: "orderNo", status: true },
            { name: "客户编码", keys: "customerNo", status: true },
            { name: "客户", keys: "customerName", status: true },
            { name: "客户类别", keys: "customerTypeDesc", status: true },
            { name: "产品编号", keys: "productCode", status: true },
            { name: "产品名称", keys: "productName", status: true },
            { name: "产品类型", keys: "productTypeDesc", status: true },
            { name: "规格", keys: "spc", status: true },
            { name: "单位", keys: "unitDesc", status: true },
            { name: "数量", keys: "qty", status: true },
            { name: "单价", keys: "price", status: true },
            { name: "金额", keys: "amount", status: true },
            { name: "仓库", keys: "warehouseDesc", status: true },
          ]
        };
      },
      computed: {
        // 表格高度
        tableHeight() {
          var fh = this.fixedHeight ? 'calc(100vh - 250px)' : void 0;

          return fh;
        },
        // 表格全屏时高度
        tableFullHeight() {
          var fh =  this.fixedHeight ? 'calc(100vh - 126px)' : null;

          return fh;
        },
      },
      methods: {
        /* 表格数据源 */
        async datasource(params) {
          params.where.pageindex = params.page;
          params.where.pagesize = params.limit;
  
          //添加排序
          if(params.order.order == null || params.order.order == undefined){
            params.where.orderBys = [{ sort: "createDate", order: 1, }];
          }else{
            params.where.orderBys = [];
            params.where.orderBys.push({sort:params.sorter.column.sortable,order:params.order.order=='asc'?0:1});
          }
        
          let data = await QuerySaleproductdetailSummaryViewByCondition(params.where);

          console.log('data',data);

          this.saleQtyTotal= data.saleQtyTotal;
          this.saleAmountTotal= data.saleAmountTotal;

          this.$nextTick(() => {
            this.$refs.table.doLayout();
          });
          
          return {
              list:data.saleproductdetailView,
              count:data.totalCount,
          };
        },
  
        /* 表格渲染完成回调 */
        onDone({ data }) {
          this.supplierList = data;
        },
        /* 刷新表格 */
        reload(where) {
          
          this.$refs.table.reload({ where: where });
        },
        
        /**
         * 显示编辑
         * @param row 当前行对像
         */
        openEdit(row) {
          removePageTab({ key: '/statistics/saleproduct/detail' });
          this.$nextTick(() => {
            this.$router.push({path: '/statistics/saleproduct/detail', query: { "productId":row.productId } });
          });
        },

        /* 导出 */
        reportexcel(pams) {

          //初始化参数
          let params = {
            pagesize: 0,
            pageindex: 1,
            conditions: {},
            orderBys: [{ sort: "createDate", order: 1, }],
          };
          //加载进度条
          const loading = this.$loading({ lock: true });
          //重新请求查询
          this.$refs.table.doRequest(({ where }) => {
          if(where){
            params.orderBys = where.orderBys;
            params.pageindex = where.pageindex;
            params.conditions = where.conditions;
          }
          //查询数据
          QuerySaleproductdetailSummaryViewByCondition(params)
            .then((data) => {
              loading.close();
              this.$refs.ExportDialog.showEdits = true
              this.$refs.ExportDialog.checkList = this.exportList
              this.$refs.ExportDialog.titleName = "销售明细列表"
              this.$refs.ExportDialog.dataList = data.saleproductdetailView;
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
          });
        },

        /* 合计 */
        getSummaries(param){
          const sums = [];
          sums[0] = '合计' ;
          const { columns, data } = param;
          columns.forEach((column, index) => {
            if(index ==12){
              sums[index] = Number(this.saleQtyTotal).toFixed(4);
            }else if(index ==14){
              sums[index] = Number(this.saleAmountTotal).toFixed(4);
            }
          });

          this.$nextTick(() => {
            this.$refs.table.doLayout();
          });
          return sums;
        },

        /* 样式 */
        cellStyle(param){
          const { row, column, rowIndex, columnIndex } = param;
          if(columnIndex==5||columnIndex==6||columnIndex==7){
            return{
              color:''
            }
          }
        },
      }
    };
  </script>
   <style lang="scss" scoped>
    /*总计字体颜色
    ::v-deep .el-table__footer-wrapper tbody td {
      color: var(--color-primary);
      cursor: pointer;
    }*/
  </style>